/**
 * 
 */
package org.gov.vis.master.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.main.Main;
import org.gov.vis.master.controller.GopalaMithraCtlr;
import org.gov.vis.master.controller.GopalaMithraCtlrImpl;
import org.gov.vis.master.dto.GopalaMithraDto;
import org.gov.vis.master.gui.EditGopalaMithraDialog;
import org.gov.vis.master.gui.ViewGopalaMithraDialog;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class EditGopalaMithraListener implements ActionListener, BaseListener{



	final private ResourceBundle rb = Util.getResources();

	private GopalaMithraCtlr gopalaMithraCtlr = new GopalaMithraCtlrImpl();

	private EditGopalaMithraDialog editGopalaMithraDialog;

	private ErrorsDisplayJPanel errorMsgPanel;

	private GopalaMithraDto gopalaMithraDto;

	public EditGopalaMithraListener(EditGopalaMithraDialog editGopalaMithraDialog) {
		// TODO Auto-generated constructor stub
		this.editGopalaMithraDialog = editGopalaMithraDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		EditGopalaMithraDialog _editGopalaMithraDialog = (EditGopalaMithraDialog)_guiObject; 
		gopalaMithraDto = new GopalaMithraDto();
		gopalaMithraDto.setCode(_editGopalaMithraDialog.getTxtbxCode().getText());
		gopalaMithraDto.setName(_editGopalaMithraDialog.getTxtbxName().getText());
		gopalaMithraDto.setFatherName(_editGopalaMithraDialog.getFatherTxtBx().getText());
		/*if(_gmithraDailog.getDobTxtBx().getText() != null && !_gmithraDailog.getDobTxtBx().getText().equals("")){
			gmDto.setDob(DU.parse(_gmithraDailog.getDobTxtBx().getText(), DBConstants.DISPLAY_DATE_FORMAT));
		}*/
		if(_editGopalaMithraDialog.getDtChoDateOfBirth().getDate() != null){
			gopalaMithraDto.setDob(_editGopalaMithraDialog.getDtChoDateOfBirth().getDate());
		}

		gopalaMithraDto.setNativeVillage(_editGopalaMithraDialog.getNativeVillageTxtBx().getText());
		gopalaMithraDto.setMandal(_editGopalaMithraDialog.getMandalTxtBx().getText());
		gopalaMithraDto.setDistrict(_editGopalaMithraDialog.getDistrictTxtBx().getText());
		gopalaMithraDto.setJurisdiction(_editGopalaMithraDialog.getJurisdiction().getText());
		gopalaMithraDto.setHeadQuarter(_editGopalaMithraDialog.getHeadQuarterTxtBx().getText());
		gopalaMithraDto.setHqDistrict(_editGopalaMithraDialog.getJTextField1().getText());
		gopalaMithraDto.setHqMandal(_editGopalaMithraDialog.getJudMandalTxtBx().getText());
		gopalaMithraDto.setGplMthId((Long)_editGopalaMithraDialog.getHidnObject());

	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {

		errorMsgPanel = new ErrorsDisplayJPanel(editGopalaMithraDialog.getjPanel1());
		errorMsgPanel.clearMsgPanel();

		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){

			addOrEditGopalaMithra();
		}

		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
			
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
			        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			    
		    if (response == JOptionPane.YES_OPTION) {
		      // User clicked YES.
		    	deleteGopalaMithra(actionEvent);
		    	String searchText = editGopalaMithraDialog.getSearchText();

				// It is basically doing back button functionality. Please, understand the logic and write the comments.
				//Replace double quotes with appropriate Dialog title.
				ViewGopalaMithraDialog viewGopalaMithraDialog = new ViewGopalaMithraDialog(Main.getMainFrame(), "View GopalaMithra", false);
				for(ActionListener actionListener: viewGopalaMithraDialog.getSearchBtn().getActionListeners()) { 
					ViewGopalaMithraListener viewGopalMithraList = (ViewGopalaMithraListener)actionListener;
					viewGopalaMithraDialog.getCodeTxtBx().setText(searchText);

					viewGopalMithraList.actionPerformed(new ActionEvent(viewGopalaMithraDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));

				}

		    	
		    } else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
		      return;
		    }

		}
	}

	private void addOrEditGopalaMithra() {


		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);


		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(editGopalaMithraDialog);

		try{
			//Method called to Add or Edit the GopalaMithra
			gopalaMithraCtlr.addOrEditGopalaMithra(gopalaMithraDto);

			JOptionPane.showMessageDialog(editGopalaMithraDialog, rb.getString(GuiConstants.MASTER_EDIT_GOPALAMTHRA_SUCCESS));
			editGopalaMithraDialog.dispose();
			
			Thread.sleep(GuiConstants.THREAD_SLEEP_TIME);

			String searchText = editGopalaMithraDialog.getSearchText();

			// It is basically doing back button functionality. Please, understand the logic and write the comments.
			//Replace double quotes with appropriate Dialog title.
			ViewGopalaMithraDialog viewGopalaMithraDialog = new ViewGopalaMithraDialog(Main.getMainFrame(), "View GopalaMithra", false);
			for(ActionListener actionListener: viewGopalaMithraDialog.getSearchBtn().getActionListeners()) { 
				ViewGopalaMithraListener viewGopalMithraList = (ViewGopalaMithraListener)actionListener;
				viewGopalaMithraDialog.getCodeTxtBx().setText(searchText);

				viewGopalMithraList.actionPerformed(new ActionEvent(viewGopalaMithraDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));

			}
		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = GopalaMithraCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;
					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_code").toUpperCase())){
						editGopalaMithraDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_name").toUpperCase())){
						editGopalaMithraDialog.getLblName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_name").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_fathername").toUpperCase())){
						editGopalaMithraDialog.getLblFatherName().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_fathername").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_gopmit_dob").toUpperCase())){
						editGopalaMithraDialog.getLblDateOfBirth().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_gopmit_dob").toUpperCase());
						errorMsgLst.add(errMsg);
					}					

					errorMsgPanel.setErrorMessages(errorMsgLst);
				} else {
					//log.debug(e.getMessage(), e);
				}	
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);

			}		
		}


	}

	private void deleteGopalaMithra(ActionEvent actionEvent) {

		Long gopalaMithrasId = (Long)editGopalaMithraDialog.getHidnObject();
		try {
			gopalaMithraCtlr.deleteGopalaMithra(gopalaMithrasId);
			JOptionPane.showMessageDialog(editGopalaMithraDialog, rb.getString(GuiConstants.MASTER_DELETE_GOPALAMTHRA_SUCCESS));
			editGopalaMithraDialog.dispose();
		} catch (Exception e) {
			List<String> errorMsgLst = new ArrayList<String>();
			errorMsgLst.add(e.getMessage());
			errorMsgPanel.setErrorMessages(errorMsgLst);

		}
	}


}
